package com.usian.wemedia.conreoller;

import com.usian.apis.wemedia.WmNewsControllerApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.vos.WmNewsVo;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/WmNews")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    WmNewsService wmNewsService;

    /**
     * 袁浩 2022-6-25
     * 文章列表查询
     * @param dto
     * @return
     */
    @PostMapping("/findall")
    @Override
    public ResponseResult findall(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findall(dto);
    }

    /**
     *袁浩 2022-6-26
     * 提交文章
     * @param wmNews
     * @return
     */
    @PostMapping("/submit")
    @Override
    public void submitNews(@RequestBody WmNews wmNews) {
        if(wmNews.getStatus()== WmNews.Status.SUBMIT.getCode()){
            //提交文章
             wmNewsService.saveNews(wmNews, WmNews.Status.SUBMIT.getCode());
        }else{
            //保存草稿
             wmNewsService.saveNews(wmNews, WmNews.Status.NORMAL.getCode());
        }
    }

    /**
     * 袁浩 2022-6-26
     * 根据ID查文章
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    @Override
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.wmNewsService(id);
    }
    /**
     * 袁浩 2022-6-26
     * 根据ID删除文章
     * @param id
     * @return
     */
    @GetMapping("/del_news/{id}")
    @Override
    public ResponseResult delByid(@PathVariable("id") Integer id) {
        return wmNewsService.delbyid(id);
    }

    /**
     * 袁浩 2022-6-26
     * 文章状态上下架
     * @param dto
     * @return
     */
    @PostMapping("/downOrUp")
    @Override
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.donworup(dto);
    }

    @GetMapping("/findOne/{id}")
    @Override
    public WmNews findById(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }

    @PostMapping("/update")
    @Override
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询所有待发布or定时发布的文章id
     * @return
     */
    @GetMapping("/findRelease")
    @Override
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    /**
     * 查询文章列表
     * @param dto
     * @return
     */
    @PostMapping("/findList")
    @Override
    public PageResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findListAndPage(dto);
    }

    /**
     *查询文章详情
     * @param id
     * @return
     */
    @GetMapping("/find_news_vo/{id}")
    @Override
    public WmNewsVo findWmNewsVo(@PathVariable("id") Integer id) {

        return  wmNewsService.findWmNewsVo(id);
    }
}
